package org.zpmis.api.http;

import org.springframework.validation.annotation.Validated;
import org.zpmis.api.business.InvoiceWebService;
import org.zpmis.common.base.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.zpmis.common.exception.BusinessException;
import org.zpmis.common.result.PlatformResult;
import org.zpmis.common.result.ResultCode;
import org.zpmis.domain.invoice.qo.*;
import org.zpmis.domain.order.qo.ApplyOrderQo;

import javax.annotation.Resource;

/**
* created with CodeGenerator
* description: 发票管理
* @author  Yang
* date: 2021-7-26
* time: 14:58:56
*/
@RestController
@RequestMapping("/invoice")
@Slf4j
public class InvoiceController extends BaseController{

    @Resource
    private InvoiceWebService invoiceWebService;


    /**
     * 申请开发票
     * @param applyInvoiceQo
     * @return
     */
    @PostMapping("/applyInvoice")
    public PlatformResult applyInvoice(@Validated @RequestBody ApplyInvoiceQo applyInvoiceQo){
        try {
            invoiceWebService.applyInvoice(applyInvoiceQo);
        } catch (BusinessException be) {
            log.error("[InvoiceController]---applyInvoice--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        }catch (Exception e) {
            log.error("[InvoiceController]---applyInvoice--->Exception:",e);
            return PlatformResult.failure();
        }
        return PlatformResult.success();
    }

    /**
     * 发票列表查询
     * @param invoiceListQo
     * @return
     */
    @PostMapping("/invoiceList")
    public PlatformResult invoiceList(@Validated @RequestBody InvoiceListQo invoiceListQo){
        try {
            return PlatformResult.success(invoiceWebService.invoiceList(invoiceListQo));
        } catch (BusinessException be) {
            log.error("[InvoiceController]---invoiceList--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        }catch (Exception e) {
            log.error("[InvoiceController]---invoiceList--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 审核发票
     * @param auditInvoiceQo
     * @return
     */
    @PostMapping("/auditInvoice")
    public PlatformResult auditInvoice(@Validated @RequestBody AuditInvoiceQo auditInvoiceQo){
        try {
            invoiceWebService.auditInvoice(auditInvoiceQo);
        } catch (BusinessException be) {
            log.error("[InvoiceController]---auditInvoice--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        }catch (Exception e) {
            log.error("[InvoiceController]---auditInvoice--->Exception:",e);
            return PlatformResult.failure();
        }
        return PlatformResult.success();
    }

    /**
     * 获取最近的一条发票信息
     * @param checkInvoiceQo
     * @return
     */
    @PostMapping("/findRecentlyInvoice")
    public PlatformResult findRecentlyInvoice(@Validated @RequestBody CheckInvoiceQo checkInvoiceQo){
        try {
            return PlatformResult.success(invoiceWebService.findRecentlyInvoice(checkInvoiceQo));
        } catch (BusinessException be) {
            log.error("[InvoiceController]---findRecentlyInvoice--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        }catch (Exception e) {
            log.error("[InvoiceController]---findRecentlyInvoice--->Exception:",e);
            return PlatformResult.failure();
        }

    }

    /**
     * 发送发票邮件
     * @param sendInvoiceEmailQo
     * @return
     */
    @PostMapping("/sendInvoiceEmail")
    public PlatformResult sendInvoiceEmail(@Validated @RequestBody SendInvoiceEmailQo sendInvoiceEmailQo){
        try {
            return PlatformResult.success(invoiceWebService.sendInvoiceEmail(sendInvoiceEmailQo));
        } catch (BusinessException be) {
            log.error("[InvoiceController]---sendInvoiceEmail--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        }catch (Exception e) {
            log.error("[InvoiceController]---sendInvoiceEmail--->Exception:",e);
            return PlatformResult.failure(ResultCode.EMAIL_SEND_ERROR);
        }

    }

}
